import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hitutor/Util/DateUtil.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';

import 'MyDialog.dart';
import 'MyImage.dart';

class AudioPlayBarLocal extends StatefulWidget {
  String path;

  AudioPlayBarLocal(this.path);

  @override
  State<StatefulWidget> createState() {
    return _AudioPlayBarLocal();
  }
}

class _AudioPlayBarLocal extends State<AudioPlayBarLocal> {
  AudioPlayer audioPlayer = AudioPlayer();
  PlayerState playerState = PlayerState.STOPPED;
  int maxMS = 0;
  int playMS = 0;

  @override
  void initState() {
    audioPlayer.onAudioPositionChanged.listen((p) {
      playMS = p.inMilliseconds;
      setState(() {});
    });
    audioPlayer.onDurationChanged.listen((Duration d) {
      maxMS = d.inMilliseconds;
      print('Max duration: $maxMS');
      setState(() {});
    });
    audioPlayer.onPlayerStateChanged.listen((event) {
      playerState = event;
      setState(() {});
      print('onPlayerStateChanged: $event');
    });
    super.initState();
  }

  play() async {
    int result = await audioPlayer.play(widget.path, isLocal: true);
    if (result == 1) {
      print('play success');
    } else {
      print('play failed');
      showToast('play failed');
    }
  }

  pause() async {
    int result = await audioPlayer.pause();
    if (result == 1) {
      // success
      print('pause success');
    } else {
      print('pause failed');
      showToast('pause failed');
    }
  }

  resume() async {
    int result = await audioPlayer.resume();
    if (result == 1) {
      // success
      print('resume success');
    } else {
      print('resume failed');
      showToast('resume failed');
    }
  }

  seek(int startMilliseconds) async {
    int result = await audioPlayer.seek(Duration(milliseconds: startMilliseconds));
    if (result == 1) {
      print('go to success');
      // await audioPlayer.resume();
    } else {
      showToast("go to failed");
      print('go to failed');
    }
  }

  @override
  void deactivate() async {
    int result = await audioPlayer.release();
    if (result == 1) {
      print('release success');
    } else {
      showToast('release failed');
    }
    super.deactivate();
  }

  playOrPause() {
    if (playerState == PlayerState.PLAYING) {
      pause();
    } else if (playerState == PlayerState.COMPLETED) {
      seek(0);
    } else if (playerState == PlayerState.PAUSED) {
      resume();
    } else {
      play();
    }
  }

  ms2time(int ms) {
    int s = (ms + 500) ~/ 1000;
    int mm = s ~/ 60;
    int ss = s % 60;
    String MM = TwoDigits(mm);
    String SS = TwoDigits(ss);
    return "$MM:$SS";
  }

  @override
  Widget build(BuildContext context) {
    double playProgress = maxMS > 0 ? (playMS / maxMS) : 0;
    String playTime = ms2time(playMS);
    String maxTime = ms2time(maxMS);
    return Row(children: [
      LocalImage(playerState == PlayerState.PLAYING ? "play_pause.png" : "play_start.png",
          width: 24, padding: EdgeInsets.all(10), onTap: playOrPause),
      Text("$playTime/$maxTime", style: TextStyle(fontSize: 11, color: MyColor.Green)),
      Expanded(
          child: Slider(
              activeColor: MyColor.Green,
              value: playProgress,
              onChanged: (value) {
                int preToMS = (maxMS * value).toInt();
                seek(preToMS);
              }))
    ]);
  }
}
